package com.imooc.controller.center;

import com.github.pagehelper.PageHelper;
import com.imooc.controller.BaseController;
import com.imooc.pojo.OrderStatus;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.vo.MyOrdersVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author gcq
 * @Create 2022-01-25
 */
@Api(value = "用户订单接口", tags = {"用户订单接口"})
@RestController
@RequestMapping("myorders")
public class MyOrdersController extends BaseController {


    /**
     * pgHelper 在分页嵌套查询时，需要手动处理嵌套分页
     *
     * @param userId
     * @param orderStatus
     * @param page
     * @param pageSize
     * @return
     */
    @ApiOperation(value = "根据用户 Id 订单状态查询用户订单", notes = "根据用户 Id 订单状态查询用户订单", httpMethod = "POST")
    @PostMapping("/query")
    public IMOOCJSONResult myOrder(@ApiParam(name = "userId", value = "用户Id", required = true)
                                   @RequestParam String userId,
                                   @ApiParam(name = "orderStatus", value = "订单状态", required = false)
                                   @RequestParam Integer orderStatus,
                                   @ApiParam(name = "page", value = "页码", required = false, defaultValue = "0")
                                   @RequestParam Integer page,
                                   @ApiParam(name = "pageSize", value = "显示条数", required = false, defaultValue = "10")
                                   @RequestParam Integer pageSize) {
        if (StringUtils.isBlank(userId)) {
            return IMOOCJSONResult.ok();
        }

        PageHelper.startPage(page, pageSize);
        List<MyOrdersVO> myOrdersVOList = myOrderService.queryMyOrder(userId, orderStatus);
        return IMOOCJSONResult.ok(getPageResult(myOrdersVOList, page));
    }

    @ApiOperation(value = "商家发货", notes = "商家发货", httpMethod = "GET")
    @GetMapping("/deliver")
    public IMOOCJSONResult deliver(@ApiParam(name = "userId", value = "用户Id", required = true)
                                   @RequestParam String orderId) {
        if (StringUtils.isBlank(orderId)) {
            return IMOOCJSONResult.errorMsg("订单Id不能为空");
        }

        myOrderService.updateDeliverOrderStatus(orderId);
        return IMOOCJSONResult.ok();
    }

    @ApiOperation(value = "用户确认收货", notes = "用户确认收货", httpMethod = "POST")
    @PostMapping("/confirmReceive")
    public IMOOCJSONResult receive(@ApiParam(name = "userId", value = "用户Id", required = true)
                                   @RequestParam String userId,
                                   @ApiParam(name = "orderId", value = "订单Id", required = true)
                                   @RequestParam String orderId) {
        if (StringUtils.isBlank(orderId) || StringUtils.isBlank(orderId)) {
            return IMOOCJSONResult.errorMsg("用户Id或订单Id不能为空");
        }

        IMOOCJSONResult checkResult = checkUserOrder(userId, orderId);
        if (checkResult.getStatus() != HttpStatus.OK.value()) {
            return checkResult;
        }

        boolean res = myOrderService.updateReceiveOrderStatus(orderId);
        if (!res) {
            return IMOOCJSONResult.errorMsg("订单确认收货状态失败");
        }

        return IMOOCJSONResult.ok();
    }


    @ApiOperation(value = "用户删除订单", notes = "用户删除订单", httpMethod = "GET")
    @GetMapping("/delete")
    public IMOOCJSONResult remove(@ApiParam(name = "userId", value = "用户Id", required = true)
                                  @RequestParam String userId,
                                  @ApiParam(name = "orderId", value = "订单Id", required = true)
                                  @RequestParam String orderId) {
        if (StringUtils.isBlank(orderId) || StringUtils.isBlank(orderId)) {
            return IMOOCJSONResult.errorMsg("用户Id或订单Id不能为空");
        }

        IMOOCJSONResult checkResult = checkUserOrder(userId, orderId);
        if (checkResult.getStatus() != HttpStatus.OK.value()) {
            return checkResult;
        }

        boolean res = myOrderService.deleteOrder(userId, orderId);
        if (!res) {
            return IMOOCJSONResult.errorMsg("订单删除失败");
        }

        return IMOOCJSONResult.ok();
    }

    @ApiOperation(value = "获得订单状态概况", notes = "获得订单状态概况", httpMethod = "POST")
    @PostMapping("/statusCounts")
    public IMOOCJSONResult saveList(@ApiParam(name = "userId", value = "用户Id", required = true)
                                    @RequestParam String userId) {
        if (StringUtils.isBlank(userId)) {
            return IMOOCJSONResult.errorMsg("用户Id不能为空");
        }
        return IMOOCJSONResult.ok(myOrderService.getOrderStatusCounts(userId));
    }

    @ApiOperation(value = "获得我的订单趋势", notes = "获得我的订单趋势", httpMethod = "POST")
    @PostMapping("/trend")
    public IMOOCJSONResult trend(@ApiParam(name = "userId", value = "用户Id", required = true)
                                 @RequestParam String userId,
                                 @ApiParam(name = "page", value = "页码", required = false, defaultValue = "0")
                                 @RequestParam Integer page,
                                 @ApiParam(name = "pageSize", value = "显示条数", required = false, defaultValue = "10")
                                 @RequestParam Integer pageSize) {
        if (StringUtils.isBlank(userId)) {
            return IMOOCJSONResult.errorMsg("用户Id不能为空");
        }
        PageHelper.startPage(page, pageSize);
        List<OrderStatus> orderStatusList = myOrderService.getMyOrderTrend(userId);
        return IMOOCJSONResult.ok(getPageResult(orderStatusList, page));
    }

}